const Product = require('../models/product');
const Cart = require('../models/cart');

module.exports.getIndex = (req, res, next) => {
    Product
        .findAll()
        .then(products => {
            res.render('shop/index', {
                pageTitle: 'Shop',
                path: '/',
                products: products
            });
        })
        .catch(err => console.log(err));
};

module.exports.getProducts = (req, res, next) => {
    Product
        .findAll()
        .then(products => {
            res.render('shop/product-list', {
                pageTitle: 'Products',
                path: '/products',
                products: products
            });
        })
        .catch(err => console.log(err));
};

module.exports.getProduct = (req, res, next) => {
    const productId = req.params.productId;
    Product
        .findAll({
            where: {
                id: productId
            }
        })
    .then(products => {
        const product = products[0];
        res.render('shop/product-details', {
            pageTitle: product.title,
            path: '/products',
            product: product
        });
    })
    .catch(err => console.log(err));

    // Product
    //     .findByPk(productId)
    //     .then(product => {
    //         res.render('shop/product-details', {
    //             pageTitle: product.title,
    //             path: '/products',
    //             product: product
    //         });
    //     })
    //     .catch(err => console.log(err));
};

module.exports.getCart = (req, res, next) => {
    req.user
        .getCart()
        .then(cart => {
            return cart.getProducts();
        })
        .then(products => {
            res.render('shop/cart', {
                pageTitle: 'Cart',
                path: '/cart',
                products: products
            });
        })
        .catch(err => console.log(err));
};

module.exports.postCart = (req, res, next) => {
    const productId = req.body.productId;
    let fetchedCart;
    let newQuantity = 1;
    req.user
        .getCart()
        .then(cart => {
            fetchedCart = cart;
            return cart
                .getProducts({
                    where: { id: productId }
                });
        })
        .then(products => {
            const product = products[0];
            if(product) {
                //const oldQuantity = product.cartItem.quantity;
                newQuantity = product.cartItem.quantity + 1;
                return product;
            } else {
                return Product
                    .findByPk(productId);
            }
        })
        .then(product => {
            return fetchedCart
                .addProduct(product, {
                    through: {
                        quantity: newQuantity
                    }
                });
        })
        .then(() => {
            res.redirect('/cart');
        })
        .catch(err => console.log(err));
};

module.exports.postDeleteCartProduct = (req, res, next) => {
    const productId = req.body.productId;
    req.user
        .getCart()
        .then(cart => {
            return cart
                .getProducts({
                    where: {
                        id: productId
                    }
                });
        })
        .then(products => {
            const product = products[0];
            return product.cartItem.destroy();
        })
        .then(result => {
            res.redirect('/cart');
        })
        .catch(err => console.log(err));
};

module.exports.getOrders = (req, res, next) => {
    req.user.
        getOrders({
            include: ['products']
        })
        .then(orders => {
            res.render('shop/orders', {
                pageTitle: 'Orders',
                path: '/orders',
                orders: orders
            });
        })
        .catch(err => console.log(err));
};

module.exports.postOrder = (req, res, next) => {
    let fetchedCart;
    req.user
        .getCart()
        .then(cart => {
            fetchedCart = cart;
            return cart.getProducts();
        })
        .then(products => {
            return req.user
                .createOrder()
                .then(order => {
                    return order.addProducts(products.map(product => {
                        product.orderItem = {
                            quantity: product.cartItem.quantity };
                        return product;
                    }));
                })
                .catch(err => console.log(err));
        })
        .then(result => {
            return fetchedCart.setProducts(null);
        })
        .then(result => {
            res.redirect('/orders');
        })
        .catch(err => console.log(err));
};